package com.fram.service.api.v1.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.user.response.UserAddressSearchResponse;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.UserReceivingAddress;
import com.fram.mapper.UserReceivingAddressMapper;
import com.fram.service.api.v1.UserAddressService;

@Service("userAddressService")
public class UserAddressServiceImpl implements UserAddressService {

	@Autowired
	private UserReceivingAddressMapper userReceivingAddressMapper;

	/**
	 * @作者：陳曉鬆 @时间：2020年4月30日 下午3:41:53 @版本：V1.0 @description：-> 查询用户的收货地址
	 */
	@Override
	public List<UserAddressSearchResponse> selectAddressListByUserId(Long userId, Integer isDefault) throws ProException {
		List<UserAddressSearchResponse> list = userReceivingAddressMapper.selectAddressListByUserId(userId, isDefault);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	/**
	 * 收货地址 增加
	 */
	@Override
	public void insertReceivingAddress(UserReceivingAddress request) throws ProException {
		if (request.getIsDefault() == 1) {
			// 根据id和默认状态 查询 （如果有便修改为0）
			List<UserAddressSearchResponse> IsDefault = userReceivingAddressMapper.selectAddressListByUserId(request.getUserId(), 1);
			for (UserAddressSearchResponse userAddressSearchResponse : IsDefault) {
				userAddressSearchResponse.setIsDefault(0);
				// 修改
				userReceivingAddressMapper.updateByPrimaryKeySelective(userAddressSearchResponse);
			}
		}
		if (userReceivingAddressMapper.insertSelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	/**
	 * 准备修改 根据 某个地址的id 主键查询
	 */
	@Override
	public UserReceivingAddress selectAddressById(Long id) throws ProException {
		// 修改 根据 全部搜索的 id 查询 相应的内容
		UserReceivingAddress userReceivingAddress = userReceivingAddressMapper.selectByPrimaryKey(id);
		if (userReceivingAddress == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return userReceivingAddress;
	}

	/**
	 * 修改地址 状态 点击确认 调转到 查询
	 */
	@Override
	public void updateReceivingAddress(UserReceivingAddress request) throws ProException {
		if (request.getIsDefault() == 1) {
			// 根据id和默认状态 查询 （如果有便修改为0）
			List<UserAddressSearchResponse> IsDefault = userReceivingAddressMapper.selectAddressListByUserId(request.getUserId(), 1);
			for (UserAddressSearchResponse userAddressSearchResponse : IsDefault) {
				userAddressSearchResponse.setIsDefault(0);
				userReceivingAddressMapper.updateByPrimaryKeySelective(userAddressSearchResponse);
			}
		}
		if (userReceivingAddressMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	/**
	 * 删除联系地址
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public void deleteAddressById(Long id) throws ProException {
		if (userReceivingAddressMapper.deleteByPrimaryKey(id) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
